import { createApp } from 'vue'
import App from './App.vue'

// 全局引用route的文件
import Router from './routers/index.js'
// 全局引用element
// import ElementPlus from 'element-plus'

// 引用api
import ApiList from './api/index.js'

// 全局引用element的index.css
// import 'element-plus/dist/index.css'

// 引用VantUI
import Vant from 'vant'

// 引用Vant的样式
import 'vant/lib/index.css'

// 引用cookie
import Cookie from 'vue3-cookies'




// createApp = 创建应用
const app = createApp(App)


    .use(Router)   //挂载路由
    .use(Vant)   //挂载UI框架
    .use(ApiList) //挂载接口
    .use(Cookie)  //挂载cookie
    .mount('#app')


Router.beforeEach(async (to,from,next) => {
    var IsLogin = to.meta.IsLogin

    if(IsLogin)
    {
         // 获取当前cookie
        var LoginUser = app.$cookies.get('LoginUser');
        // cookie为空 需要登录
        if (!LoginUser) {
            app.$notify({
                type: "danger",
                message: '请先登录',
                duration:1500
            });
            next('/user/base/login')
            return false
        }

        var userid = LoginUser.id ? LoginUser.id : 0;

        // 发起请求
        var result = await app.$api.check({userid:userid})

        if(result.code === 1)
        {
            app.$cookies.set('LoginUser',result.data)
            next()
        }else{
            app.$notify({
                type: "danger",
                message: result.msg,
                duration:1500,
                
            });
            app.$cookies.remove('LoginUser')
            next('/user/base/login')
            return false
        }
    }else{
        next()
    }
})
